// initial values of location and player coordinates need to be fixed

#include <iostream>
#include <ctime>
#include "player.h"
#include "location.h"
#include "bank.h"
#include "ccnc.h"
#include "iGraphics.h"
#include <cstring>
#include <cstdio>
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#define display iClear();iShowBMP(0, 256, "5-visitjail.bmp");iShowBMP(128, 256, "4-chance.bmp");iShowBMP(192, 256, "3-euston.bmp");iShowBMP(256, 256, "2-kingscross.bmp");iShowBMP(320, 256, "1-whitechapel.bmp");iShowBMP(384, 256, "0-go.bmp");iShowBMP(384, 384, "19-mayfair.bmp");iShowBMP(384, 448, "18-commu.bmp");iShowBMP(384, 512, "17-liverpool.bmp");iShowBMP(384, 576, "16-bond.bmp");iShowBMP(384, 640, "15-jail.bmp");iShowBMP(320, 640, "14-piccadilly.bmp");iShowBMP(256, 640, "13-water.bmp");iShowBMP(192, 640, "12-fench.bmp");iShowBMP(128, 640, "11-chance.bmp");iShowBMP(0, 640, "10-parking.bmp");iShowBMP(0, 576, "9-commu.bmp");iShowBMP(0, 512, "8-bow.bmp");iShowBMP(0, 448, "7-mary.bmp");iShowBMP(0, 384, "6-electric.bmp")
using namespace std;
int i, j, k; // for array indexing
int n = 2; //no. of players;
bank b(n);
char output_string[1000];
char current_player_disp[1000];
char current_loc_name[1000];
char current_loc_owner[1000];
char current_loc_price[1000];
char current_loc_rent[1000];
char money_string0[100];
char money_string1[100];
char money_string2[100];
char money_string3[100];
char worth_string0[100];
char worth_string1[100];
char worth_string2[100];
char worth_string3[100];
char end_game_string[1000];
player p[4] = { 0, 1, 2, 3 };
location l[20];
int flag = 1;
ccnc card;
int roll_flag = 0;
int mort_flag = 0;
int buyback_flag = 0;
int rent_flag = 0;
int buy_flag = 0;
int end_game_flag = 0;
int main_menu_flag = 1;
int main_x = 100;
int exit_flag;
int dice_value;
int arrowx = 1095;
int arrowy = 275;
int p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y;
int counter;
int player_index = -1; //use player_index-1 for referencing;
void iDraw()
{
	iClear();
	if (main_menu_flag)
	{
		if (exit_flag == 1) exit(0);
		if (main_x == 100) iShowBMP(0, 0, "menuplay.bmp");
		else iShowBMP(0, 0, "menuexit.bmp");
	}
	else
	{
		if (end_game_flag == 1)
		{

			iShowBMP(0, 0, "bgtest.bmp");
			int i, net_worth_arr[4], max = 0, maxi;
			net_worth_arr[0] = p[0].net_worth(l);
			net_worth_arr[1] = p[1].net_worth(l);
			net_worth_arr[2] = p[2].net_worth(l);
			net_worth_arr[3] = p[3].net_worth(l);
			for (i = 0; i < 4; i++)
			{
				if (net_worth_arr[i] > max)
				{
					max = net_worth_arr[i];
					maxi = i;
				}
			}
			sprintf_s(end_game_string, "PLAYER %d WON THE GAME WITH A TOTAL WORTH OF %d!", maxi + 1, max);
			iSetColor(0, 0, 0);
			iText(367, 394, end_game_string, GLUT_BITMAP_TIMES_ROMAN_24);
		}
		else
		{
			iShowBMP(0, 0, "sample.bmp");
			iShowBMP(0, 256, "5-visitjail.bmp");
			iShowBMP(128, 256, "4-chance.bmp");
			iShowBMP(192, 256, "3-euston.bmp");
			iShowBMP(256, 256, "2-kingscross.bmp");
			iShowBMP(320, 256, "1-whitechapel.bmp");
			iShowBMP(384, 256, "0-go.bmp");
			iShowBMP(384, 384, "19-mayfair.bmp");
			iShowBMP(384, 448, "18-commu.bmp");
			iShowBMP(384, 512, "17-liverpool.bmp");
			iShowBMP(384, 576, "16-bond.bmp");
			iShowBMP(384, 640, "15-jail.bmp");
			iShowBMP(320, 640, "14-piccadilly.bmp");
			iShowBMP(256, 640, "13-water.bmp");
			iShowBMP(192, 640, "12-fench.bmp");
			iShowBMP(128, 640, "11-chance.bmp");
			iShowBMP(0, 640, "10-parking.bmp");
			iShowBMP(0, 576, "9-commu.bmp");
			iShowBMP(0, 512, "8-bow.bmp");
			iShowBMP(0, 448, "7-mary.bmp");
			iShowBMP(0, 384, "6-electric.bmp");
			/*strcpy_s(output_string, card.community_chest(p[0], b, 4));
			iText(40, 128, output_string, GLUT_BITMAP_TIMES_ROMAN_24);
			strcpy_s(output_string, card.chance(p[0], b, 1));
			iText(40, 28, output_string, GLUT_BITMAP_TIMES_ROMAN_24);*/
			//iRectangle(1166, 250, 180, 60); //Option rectangles
			//iRectangle(1166, 190, 180, 60);
			//iRectangle(1166, 130, 180, 60);
			//iRectangle(1166, 70, 180, 60);
			iText(1167, 280, "ROLL NEXT PLAYER", GLUT_BITMAP_HELVETICA_18);
			iText(1226, 220, "BUY LOT", GLUT_BITMAP_HELVETICA_18);
			iText(1200, 160, "FINISH GAME", GLUT_BITMAP_HELVETICA_18);
			//iText(1226, 100, "BUY BACK", GLUT_BITMAP_HELVETICA_18);

			
			if (!(p[0].is_eliminated))
			{
				//iShowBMP(626, 576, "p0c.bmp"); //PLAYER CARDS
				iText(710, 675, "PLAYER 1", GLUT_BITMAP_HELVETICA_18);
				sprintf_s(money_string0, "MONEY: %d", p[0].money);
				iText(690, 650, money_string0, GLUT_BITMAP_HELVETICA_18);
				sprintf_s(worth_string0, "NET WORTH: %d", p[0].net_worth(l));
				iText(680, 625, worth_string0, GLUT_BITMAP_HELVETICA_18);
				if (p[0].is_jail)iText(675, 600, "CURRENTLY IN JAIL", GLUT_BITMAP_HELVETICA_18);
			}
			else
			{
				iText(680, 625, "ELIMINATED", GLUT_BITMAP_HELVETICA_18);
			}

			if (!(p[1].is_eliminated))
			{
				//iShowBMP(996, 576, "p1c.bmp");
				iText(1080, 675, "PLAYER 2", GLUT_BITMAP_HELVETICA_18);
				sprintf_s(money_string1, "MONEY: %d", p[1].money);
				iText(1060, 650, money_string1, GLUT_BITMAP_HELVETICA_18);
				sprintf_s(worth_string1, "NET WORTH: %d", p[1].net_worth(l));
				iText(1050, 625, worth_string1, GLUT_BITMAP_HELVETICA_18);
				if (p[1].is_jail)iText(1045, 600, "CURRENTLY IN JAIL", GLUT_BITMAP_HELVETICA_18);
			}
			else
			{
				iText(1050, 625, "ELIMINATED", GLUT_BITMAP_HELVETICA_18);
			}

			if (!(p[2].is_eliminated))
			{
				//iShowBMP(626, 384, "p2c.bmp");
				iText(710, 483, "PLAYER 3", GLUT_BITMAP_HELVETICA_18);
				sprintf_s(money_string2, "MONEY: %d", p[2].money);
				iText(690, 458, money_string2, GLUT_BITMAP_HELVETICA_18);
				sprintf_s(worth_string2, "NET WORTH: %d", p[2].net_worth(l));
				iText(680, 433, worth_string2, GLUT_BITMAP_HELVETICA_18);
				if (p[2].is_jail)iText(675, 408, "CURRENTLY IN JAIL", GLUT_BITMAP_HELVETICA_18);
			}
			else
			{
				iText(680, 433, "ELIMINATED", GLUT_BITMAP_HELVETICA_18);
			}

			if (!(p[3].is_eliminated))
			{
				//iShowBMP(996, 384, "p3c.bmp");
				iText(1080, 483, "PLAYER 4", GLUT_BITMAP_HELVETICA_18);
				sprintf_s(money_string3, "MONEY: %d", p[3].money);
				iText(1060, 458, money_string3, GLUT_BITMAP_HELVETICA_18);
				sprintf_s(worth_string3, "NET WORTH: %d", p[3].net_worth(l));
				iText(1050, 433, worth_string3, GLUT_BITMAP_HELVETICA_18);
				if (p[3].is_jail)iText(1045, 408, "CURRENTLY IN JAIL", GLUT_BITMAP_HELVETICA_18);
			}
			else
			{
				iText(1050, 433, "ELIMINATED", GLUT_BITMAP_HELVETICA_18);
			}


			iShowBMP(arrowx, arrowy, "arrow.bmp");
			for (i = 0; i<4; i++) //token for loop
			{
				if ((p[i].current_location_code == 0) || (p[i].current_location_code == 5) || (p[i].current_location_code == 10) || (p[i].current_location_code == 15))
				{
					if (i == 0)iShowBMP(l[p[i].current_location_code].x + 32, l[p[i].current_location_code].y + 56, "p0.bmp");
					if (i == 1)iShowBMP(l[p[i].current_location_code].x + 48, l[p[i].current_location_code].y + 56, "p1.bmp");
					if (i == 2)iShowBMP(l[p[i].current_location_code].x + 64, l[p[i].current_location_code].y + 56, "p2.bmp");
					if (i == 3)iShowBMP(l[p[i].current_location_code].x + 80, l[p[i].current_location_code].y + 56, "p3.bmp");
				}
				if (p[i].current_location_code > 0 && p[i].current_location_code < 5)
				{
					if (i == 0)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y, "p0.bmp");
					if (i == 1)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 16, "p1.bmp");
					if (i == 2)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 32, "p2.bmp");
					if (i == 3)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 48, "p3.bmp");
				}
				if (p[i].current_location_code>5 && p[i].current_location_code < 10)
				{
					if (i == 0)iShowBMP(l[p[i].current_location_code].x, l[p[i].current_location_code].y + 24, "p0.bmp");
					if (i == 1)iShowBMP(l[p[i].current_location_code].x + 16, l[p[i].current_location_code].y + 24, "p1.bmp");
					if (i == 2)iShowBMP(l[p[i].current_location_code].x + 32, l[p[i].current_location_code].y + 24, "p2.bmp");
					if (i == 3)iShowBMP(l[p[i].current_location_code].x + 48, l[p[i].current_location_code].y + 24, "p3.bmp");
				}
				if (p[i].current_location_code>10 && p[i].current_location_code < 15)
				{
					if (i == 0)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 112, "p0.bmp");
					if (i == 1)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 96, "p1.bmp");
					if (i == 2)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 80, "p2.bmp");
					if (i == 3)iShowBMP(l[p[i].current_location_code].x + 24, l[p[i].current_location_code].y + 64, "p3.bmp");
				}
				if (p[i].current_location_code>15 && p[i].current_location_code < 20)
				{
					if (i == 0)iShowBMP(l[p[i].current_location_code].x + 112, l[p[i].current_location_code].y + 24, "p0.bmp");
					if (i == 1)iShowBMP(l[p[i].current_location_code].x + 96, l[p[i].current_location_code].y + 24, "p1.bmp");
					if (i == 2)iShowBMP(l[p[i].current_location_code].x + 80, l[p[i].current_location_code].y + 24, "p2.bmp");
					if (i == 3)iShowBMP(l[p[i].current_location_code].x + 64, l[p[i].current_location_code].y + 24, "p3.bmp");
				}
			}
			for (i = 0; i < 20; i++) //for property indicator
			{

				if (i>0 && i < 5)
				{
					if (l[i].owner_code == 0)iShowBMP(l[i].x + 16, l[i].y + 132, "p0p0.bmp");
					if (l[i].owner_code == 1)iShowBMP(l[i].x + 16, l[i].y + 132, "p1p0.bmp");
					if (l[i].owner_code == 2)iShowBMP(l[i].x + 16, l[i].y + 132, "p2p0.bmp");
					if (l[i].owner_code == 3)iShowBMP(l[i].x + 16, l[i].y + 132, "p3p0.bmp");
				}
				if (i>5 && i < 10)
				{
					if (l[i].owner_code == 0)iShowBMP(l[i].x + 132, l[i].y + 16, "p0p1.bmp");
					if (l[i].owner_code == 1)iShowBMP(l[i].x + 132, l[i].y + 16, "p1p1.bmp");
					if (l[i].owner_code == 2)iShowBMP(l[i].x + 132, l[i].y + 16, "p2p1.bmp");
					if (l[i].owner_code == 3)iShowBMP(l[i].x + 132, l[i].y + 16, "p3p1.bmp");
				}
				if (i>10 && i < 15)
				{
					if (l[i].owner_code == 0)iShowBMP(l[i].x + 16, l[i].y - 12, "p0p0.bmp");
					if (l[i].owner_code == 1)iShowBMP(l[i].x + 16, l[i].y - 12, "p1p0.bmp");
					if (l[i].owner_code == 2)iShowBMP(l[i].x + 16, l[i].y - 12, "p2p0.bmp");
					if (l[i].owner_code == 3)iShowBMP(l[i].x + 16, l[i].y - 12, "p3p0.bmp");
				}
				if (i>15 && i < 20)
				{
					if (l[i].owner_code == 0)iShowBMP(l[i].x - 12, l[i].y + 16, "p0p1.bmp");
					if (l[i].owner_code == 1)iShowBMP(l[i].x - 12, l[i].y + 16, "p1p1.bmp");
					if (l[i].owner_code == 2)iShowBMP(l[i].x - 12, l[i].y + 16, "p2p1.bmp");
					if (l[i].owner_code == 3)iShowBMP(l[i].x - 12, l[i].y + 16, "p3p1.bmp");
				}
			}
			if (player_index >= 0)//dont remove brace, to make local, MIDDLE CARD
			{
				iRectangle(152, 408, 208, 208);
				int temp_player;
				if (player_index == 0)temp_player = 3;
				else temp_player = player_index - 1;
				int present_location;
				present_location = p[temp_player].current_location_code;
				sprintf_s(current_loc_name, "%s", l[present_location].name);

				iText(172, 586, current_loc_name);
				if (l[present_location].is_property)
				{
					sprintf_s(current_loc_price, "Price: $%d", l[present_location].price);
					sprintf_s(current_loc_rent, "Rent: $%d", l[present_location].rent);
					iText(172, 556, current_loc_price);
					iText(172, 526, current_loc_rent);
					if (l[present_location].owner_code > -1)
					{
						sprintf_s(current_loc_owner, "Owner: Player %d", l[present_location].owner_code + 1);
						iText(172, 496, current_loc_owner);
					}
				}

			}

			if (player_index != 0)sprintf_s(current_player_disp, "Current Player: %d", player_index);
			else sprintf_s(current_player_disp, "Current Player: 4");
			if (player_index != -1)iText(1170, 340, current_player_disp, GLUT_BITMAP_HELVETICA_18);
			else iText(1140, 340, "First Player Yet to Roll ", GLUT_BITMAP_HELVETICA_18);
			if (player_index < 4)
			{
				if (roll_flag)
				{
					if (p[player_index].is_eliminated)
					{
						int count_end = 0;
						while (p[player_index].is_eliminated == 1)
						{
							player_index++;
							count_end++;
							if (count_end == 3)
							{
								end_game_flag++;
								break;
							}
							if (player_index == 4)player_index = 0;
						}
					}

					if (player_index == -1)player_index++; //exceptional case during starting of game
					if (p[player_index].is_jail)
					{
						if (p[player_index].is_get_out_card)
						{
							p[player_index].is_get_out_card--;
							p[player_index].is_jail = 0;
							p[player_index].turns_since_free = 0;
							sprintf_s(output_string, "Player %d used get out of jail free card.", player_index + 1);
						}
						else if (p[player_index].turns_since_free)
						{
							p[player_index].turns_since_free--;
							if (p[player_index].turns_since_free == 0)
							{
								p[player_index].is_jail = 0;
								if (p[player_index].money < 50)
								{
									p[player_index].is_eliminated = 1;
									for (int x = 0; x < 20; x++)
									{
										if (p[player_index].properties[x] == 1)
										{
											p[player_index].properties[x] = 0;
											l[x].owner_code = -1;
											b.fund += l[x].price / 2;
										}
									}
									sprintf_s(output_string, "Couldn't pay. Eliminated next");
								}
								else
								{
									p[player_index].money -= 50;
									b.fund += 50;
									sprintf_s(output_string, "Player %d got out of jail paying $50!", player_index + 1);
								}

							}
							else
							{
								sprintf_s(output_string, "Player %d is in jail. Wait %d turns", player_index + 1, p[player_index].turns_since_free);
								//player_index++;
							}

						}
					}
					else
					{
						strcpy_s(output_string, p[player_index].rand_dice(player_index, l));
						dice_value = player_index;
						int present_location = p[player_index].current_location_code;
						if (present_location == 4 || present_location == 11)//chance
						{
							strcat_s(output_string, card.chance(p[player_index], b, l));
						}
						else if (present_location == 9 || present_location == 18)//community chest
						{
							strcat_s(output_string, card.community_chest(p[player_index], b, l));
						}
						if ((l[present_location].owner_code >= 0) && (l[present_location].owner_code != player_index) && (p[l[present_location].owner_code].properties[present_location]>0)) //FOR PAYING RENT
						{
							strcat_s(output_string, b.paying_rent(p[player_index], p[l[present_location].owner_code], l[present_location], l));
						}
					}



					iText(10, 83, output_string, GLUT_BITMAP_HELVETICA_18);
					player_index++;
					roll_flag = 0;
				}
				if (p[dice_value].move == 1) iShowBMP(900, 182, "1 die.bmp");
				if (p[dice_value].move == 2) iShowBMP(900, 182, "2 die.bmp");
				if (p[dice_value].move == 3) iShowBMP(900, 182, "3 die.bmp");
				if (p[dice_value].move == 4) iShowBMP(900, 182, "4 die.bmp");
				if (p[dice_value].move == 5) iShowBMP(900, 182, "5 die.bmp");
				if (p[dice_value].move == 6) iShowBMP(900, 182, "6 die.bmp");
			}
			if (player_index >= 4)player_index = 0;

			//buying function
			if (buy_flag == 1)
			{
				int temp_player;
				if (player_index == 0)temp_player = 3;
				else temp_player = player_index - 1;
				int present_location;
				present_location = p[temp_player].current_location_code;
				if (temp_player<0)
				{
					sprintf_s(output_string, "No player selected yet");
				}
				else if (!l[present_location].is_property) //is it a property 
				{
					sprintf_s(output_string, "Not a property");
				}
				else if (l[present_location].owner_code > -1)
				{
					sprintf_s(output_string, "Property already owned");
				}
				else if (b.buying_property(temp_player, present_location, p[temp_player], l[present_location]))
				{
					sprintf_s(output_string, "Congratulations! Player %d has bought %s for $%d", temp_player + 1, l[present_location].name, l[present_location].price);
				}
				else
				{
					sprintf_s(output_string, "Sorry! Not enough money");
				}
				buy_flag = 0;

			}
			/*for (i = 0; i < 4; i++)
			{
			//display;
			strcpy_s(output_string, p[i].rand_dice(i, l));
			iText(10, 128, output_string, GLUT_BITMAP_HELVETICA_18);
			if (flag == 0)iDelay(1);
			}*/


			iText(10, 83, output_string, GLUT_BITMAP_HELVETICA_18);
		}
	}
}

void iMouseMove(int mx, int my)
{
	//
}

void iMouse(int button, int state, int mx, int my)
{
	//
}

void iKeyboard(unsigned char key)
{
	if (key == '\r' && main_menu_flag == 0)
	{
		if (arrowy == 275)roll_flag = 1;
	}
	if (key == '\r' && main_menu_flag == 0)
	{
		if (arrowy == 215)buy_flag = 1;
	}
	if (key == '\r' && main_menu_flag == 0)
	{
		if (arrowy == 155)end_game_flag = 1;
	}
	if (key == '\r')
	{
		if (main_x == 100) main_menu_flag = 0;
		else if (main_x == 200) exit_flag = 1;
	}
}

void iSpecialKeyboard(unsigned char key)
{
	if (key == GLUT_KEY_DOWN)
	{
		if (main_menu_flag == 0 && end_game_flag == 0)
		{
			if (arrowy != 155)arrowy -= 60;
			else arrowy = 275;
		}
		else
		{
			if (main_x == 100) main_x = 200;
			else main_x = 100;
		}
	}
	if (key == GLUT_KEY_UP)
	{
		if (main_menu_flag == 0 && end_game_flag == 0)
		{
			if (arrowy != 275)arrowy += 60;
			else arrowy = 155;
		}
		else
		{
			if (main_x == 100) main_x = 200;
			else main_x = 100;
		}
		
	}
	if (key == GLUT_KEY_LEFT)
	{
		if (main_x == 100) main_x = 200;
		else main_x = 100;
	}
	if (key == GLUT_KEY_RIGHT)
	{
		if (main_x == 100) main_x = 200;
		else main_x = 100;
	}
}



int main()
{
	srand(time(NULL));
	//p[1].money = 250;
	//p[2].money = 2;
	//p[3].money = 2;
	strcpy_s(output_string, "Welcome to Monopoly!");
	card.hardcode_location(l);
	//PlaySound("Music.wav", NULL, SND_LOOP | SND_ASYNC); //MUSIC
	iInitialize(1366, 768, "Monopoly");
	return 0;

}

